<?php

namespace app\admin\controller;

use app\common\model\ChipsModel;

class ChipsController extends BaseController
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $model = new ChipsModel();
            return json([
                'count' => $model->count(),
                'list' => $model->order('sort', 'desc')
                    ->page((int)$this->get('page', 1), (int)$this->get('limit', 20))
                    ->select()
                    ->toArray(),
                'code' => 0,
            ]);
        }
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            if (!in_array($pay_type = trim($this->post('pay_type', '')), [1, 2])) {
                //return static::bulidFail('支付类型非法');
            }

            if (!in_array($app_scenarios = trim($this->post('app_scenarios', '')), [1])) {
                //return static::bulidFail('应用场景非法');
            }

            if (!in_array($status = $this->post('status', 0), [0, 1])) {
                return static::bulidFail('状态非法');
            }

            if (!($amount = trim($this->post('amount', ''))) || !is_numeric($amount)) {
                return static::bulidFail('筹码面额非法');
            }

            if (!is_numeric($sort = $this->post('sort', 0))) {
                return static::bulidFail('权重非法');
            }

            $model = new ChipsModel();
            $model->pay_type = 1;
            $model->app_scenarios = 1;
            $model->amount = $amount;
            $model->sort = $sort;
            $model->status = $status;
            $model->operator = $this->admin_name;
            if (!$model->save()) {
                return static::bulidFail();
            }
            //操作日志
            $log_content = "添加筹码配置，ID：{$model->id}，筹码面额：{$model->amount}";
            parent::addAdminLog($log_content);

            return static::bulidSuccess();
        }

        return $this->fetch();
    }

    public function edit()
    {
        if (!($id = $this->get('id', '')) || !is_numeric($id)) {
            return static::bulidFail('ID错误');
        }

        if (!$model = ChipsModel::where('id', $id)->find()) {
            return static::bulidFail('不存在的筹码');
        }

        if ($this->request->isAjax()) {

            if (!in_array($status = $this->post('status', 0), [0, 1])) {
                return static::bulidFail('状态非法');
            }

            if (!($amount = trim($this->post('amount', ''))) || !is_numeric($amount)) {
                return static::bulidFail('筹码面额非法');
            }

            if (!is_numeric($sort = $this->post('sort', 0))) {
                return static::bulidFail('权重非法');
            }

            $model->amount = $amount;
            $model->sort = $sort;
            $model->status = $status;
            $model->operator = $this->admin_name;

            if (!$model->save()) {
                return static::bulidFail();
            }

            //操作日志
            $log_content = "编辑筹码配置，ID：{$model->id}，筹码面额：{$model->amount}";
            parent::addAdminLog($log_content);

            return static::bulidSuccess();
        }

        return $this->assign([
            'model' => $model,
        ])->fetch();
    }

    public function del()
    {
        if (!($id = $this->post('id')) || !is_numeric($id)) {
            return static::bulidFail('ID错误');
        }

        if (!$model = ChipsModel::where('id', $id)->find()) {
            return static::bulidFail('不存在的筹码 请勿非法操作');
        }

        if (!$model->delete()) {
            return static::bulidFail('删除失败');
        }
        //操作日志
        $log_content = "删除筹码配置，ID：{$model->id}，筹码面额：{$model->amount}";
        parent::addAdminLog($log_content);
        return static::bulidSuccess('已删除');
    }
}